<?php
	defined( "AZ_EXE_" ) or die ( "ERROR : CORE VARIABLE IS NOT DEFINED." );
	class Element extends AZ {
		private $tag = "" ;
		private $attributes = array();
		private $elements = array();
		private $value = "" ;
		public function Element( $tag = false , $attributes = array() , $value = "" ){
			if ( $tag != false && $tag != "" ){
				$this->tag = $tag ;
				$this->attributes = $attributes ;
				$this->value = $value ;
			}
		}
		public function getInstance( $tag = false , $attributes = array() , $value = "" ){
			return new Element($tag,$attributes,$value);
		}
		public function setTag($tag = false){
			if ( $tag != false && $tag != "" ){
				$this->tag = $tag ;
			}
		}
		public function getTag(){
			return $this->tag ;
		}
		private function checkAttributes( $attributes ){
			if ( is_array($attributes) && !empty($attributes) ){
				foreach( $attributes AS $attribute ){
					if ( is_array($attribute) && !empty($attribute) ){
						$this->attributes = $attributes ;
						return;
					}
				}
				$this->attributes = $attributes ;
			}
		}
		public function setValue( $value ){
			$this->value = $value ;
			$this->elements=array();
		}
		public function getValue( ){
			return $this->value ;
		}
		public function addElement($element){
			if ( $element instanceof Element ){
				$this->elements[]=$element;
			}//print_r($this->elements);die();
		}
		public function addAttr( $attribute = array() ){
			foreach ( $attribute AS $key => $value ){
				if ( array_key_exists( $key , $this->attributes ) ){
					$this->attributes[$key] = $value ;
				}
				else{
					$this->attributes[$key] = $value;
				}
			}
		}
		public function setAttrs( $attributes = array() ){
			$this->attributes= array();
			foreach ( $attributes AS $key => $value ){
				if ( array_key_exists( $key , $this->attributes ) ){
					$this->attributes[$key] = $value ;
				}
				else{
					$this->attributes[$key] = $value;
				}
			}
		}
		public function getAttr( $attributeName ){
			if ( array_key_exists( $attributeName , $this->attributes ) ){
				return $this->attributes[$attributeName];
			}
			return false ;
		}
		public function getAttrs(){
			return $this->attributes ;
		}
		public function getElement(){//die(print_r($this->elements));
			if ( $this->tag != "" && $this->tag != false ){
				$elements = "" ;
				foreach( $this->elements as $element ){
					$elements .= $element->getElement();
				}
				$element = "<$this->tag ";
				if ( is_array( $this->attributes ) ){
					foreach( $this->attributes AS $key => $value ){
						$element .= " $key=\"$value\" " ;
					}
				}
				$element .=" >" . ($elements==""?$this->value:$elements) . "</$this->tag>" ;
				return $element ;
			}
			return "Missing tag name";
		}
	}
?>